package com.mvc.core.util;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.mvc.core.annotation.Controller;
import com.mvc.core.annotation.RequestMapping;
import com.mvc.core.annotation.RequestParam;

public class InitAnnotation {
	
	private static final String PACKAGE_NAME = "com.mvc";
	
	public static Map<String,Request> initConfig(){
		Map<String,Request> requestMap = new HashMap<>();
		Set<Class<?>> classes = PackagesScanUtil.getClasses(PACKAGE_NAME);
		for (Class<?> classzz : classes) {
			if(classzz.isAnnotationPresent(Controller.class)){
				Method [] methods = classzz.getMethods();
				for (Method method : methods) {
					if(method.isAnnotationPresent(RequestMapping.class)){
						Request request = new Request();
						request.setClassName(classzz.getName());
						RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
						Class<?> returnClass = method.getReturnType(); //获取方法的返回类型
						request.setRequestUrl(requestMapping.value());
						request.setRequestMothod(method.getName());
						request.setReturnObj(returnClass.getName());
						Class<?>[] parameterTypes = method.getParameterTypes();
						if(parameterTypes != null && parameterTypes.length > 0){
							request.setParameterTypes(parameterTypes);
//							for (Class<?> parameterType : parameterTypes) {
//								if(parameterType.isAnnotationPresent(RequestParam.class)){
//									RequestParam requestParam = parameterType.getAnnotation(RequestParam.class);
//									RequestParamer paramer = new RequestParamer();
//									if(StringUtils.isNotBlank(requestParam.value())){
//										paramer.setParamerName(requestParam.value());
//									}
//									paramer.setParamerType(parameterType);
//									request.setRequestParamer(paramer);
//								}
//							}
						}
						requestMap.put(requestMapping.value(), request); //将请求的信息封装到MAP中
					}
				}
			}else{
				System.out.println("该类不是有效类");
			}
		}
		return requestMap;
		
	}

}
